package io.github.harmonly.controller.api;

import io.github.harmonly.entity.activity.Star;
import io.github.harmonly.service.StarService;
import io.github.harmonly.util.JSONUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/api")
public class StarController {

    @Resource
    private StarService service;

    @ResponseBody
    @RequestMapping(value = "/stars", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getStars() {
        return JSONUtil.toJSONString(service.getStars());
    }

    @ResponseBody
    @RequestMapping(value = "/star/{sid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getStar(@PathVariable("sid") int sid) {
        return JSONUtil.toJSONString(service.getStar(sid));
    }

    @ResponseBody
    @RequestMapping(value = "/star", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public String addStar(@RequestBody String json) {
        return JSONUtil.result(service.addStar(JSONUtil.parseObject(json, Star.class)));
    }

    @ResponseBody
    @RequestMapping(value = "/star/{sid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.DELETE)
    public String deleteStar(@PathVariable("sid") int sid) {
        return JSONUtil.result(service.deleteStar(sid));
    }
}
